
def cal_spacing(rule,length,num=None):
    head = []
    end = []
    body = []
    body_width = False
    for r in rule.split(','):
        if r[0] == '<':
            head.append(int(r[1:]))
        elif r[0] == '>':
            end.append(int(r[1:]))
        elif r[0] == '~':
            body_width = int(r[1:])
    if body_width:
        length = length - sum(head) - sum(end)
        count = length /500 +(length % 500 >0 and 1 or 0)
        if count >0:
            width = float(length) / count
            for i in range(count):
                body.append(width)
    elif num:
        width = float(length) / (num-1)
        print(width)
        for i in range(num-1):
            body.append(width)
    result =  head + body + end
    return {'layout':result,'qty':len(result)+1}


# import re
# re.compile(r'\d+')
# def cal_spacing2(rules_str,length):
#     result = []
#     for rule_str in rules_str.split('|'):
#         array = []
#         numbers = rule_str.split(',')
#         wait_todo = []
#         lwh = re.findall(r'\d+',self.spec_name)
#         for num in numbers:
#             if num[0] = '~':


if __name__ == '__main__':
    length = 1200
    rule_str = '<100,<200,~500,>200,>100'
    print(cal_spacing(rule_str,length,None))
    rule_str = ':'
    print(cal_spacing(rule_str,300,4))

    # rule_str = '100,200,|,:1,:2,:1,|,200,100'
    # rule_str = '100,200|~500|200,100'
    # print(cal_spacing2(rule_str,length))
